package com.hello.interceptors;

import java.util.Calendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class TimeControlInterceptor extends HandlerInterceptorAdapter {
	
	private int openingTime;
	private int closingTime;
	
	public void setOpeningTime(int openingTime) {
		this.openingTime = openingTime;
	}
	
	public void setClosingTime(int closingTime) {
		this.closingTime = closingTime;
	}

	@Override
	public boolean preHandle(HttpServletRequest request,
								HttpServletResponse response, Object handler) throws Exception {
		Calendar cal = Calendar.getInstance();
		int hour = cal.get(Calendar.HOUR_OF_DAY);
		if(openingTime <= hour && hour < closingTime) {
			return true;
		} else {
			response.sendRedirect("pageOutOfTime.html");
			return false;
		}
	}
	
	
}
